/**

 * @author admin

 * 2014年7月14日

 */

package com.tmall.testgen.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class FileUtil {

	public static String readFile(String path) throws Exception{
		File file = new File(path);
		InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"UTF-8");
        char[] temp=new char[(int) file.length()];
        isr.read(temp);
        isr.close();
        
        return new String(temp);
        
	}
	
	public static void createFile(String filePath,String content,boolean deleteOldFile) throws Exception{
	    
	    
	    filePath=StringUtil.normalizePath(filePath);
	    if(filePath.endsWith("/"))
	        filePath=filePath.substring(0, filePath.length()-1);
	    
	    String fileRootPath=filePath.substring(0, filePath.lastIndexOf("/"));
	    File fileRootFolder=new File(fileRootPath);
	    fileRootFolder.mkdirs();
	    
        File f = new File(filePath);
        //如果已经存在且不需要删除源文件则不做任何操作
        if(f.exists()&&!deleteOldFile){
            return;
        }
       
        if (f.exists()&&deleteOldFile) {
         f.delete();
        }else{
           f.createNewFile();
        }
        FileWriter fw = new FileWriter(f);
        PrintWriter pw = new PrintWriter(fw);
        pw.append(content);
        
        pw.flush();
        pw.close();
	    
	}
	public static void copyLib(String sourcePath,String targetFolderPath) throws Exception{
	    targetFolderPath=StringUtil.normalizePath(targetFolderPath);
	    sourcePath=StringUtil.normalizePath(sourcePath);
	    
	    
	    if(!targetFolderPath.endsWith("/"))
            targetFolderPath=targetFolderPath+"/";
	    
	    String fileName=sourcePath;
	    int pos=sourcePath.lastIndexOf("/");
	    if(pos>0)
	        fileName=fileName.substring(pos+1);
	    
	    File targetFile=new File(targetFolderPath+fileName);
	    if(targetFile.exists())
	        return;
	    
	    File targetFolder=new File(targetFolderPath);
	    targetFolder.mkdirs();
	    
	   
        InputStream in = FileUtil.class.getResourceAsStream(sourcePath);  
        if (in == null)  
            return;
        
        
        
        
        
        byte[] buffer = new byte[512];  
        BufferedOutputStream out = null;  
        try  
        {  
            try  
            {  
                out = new BufferedOutputStream(new FileOutputStream(targetFile));  
                while (true)  
                {  
                    int readed = in.read(buffer);  
                    if (readed > -1)  
                    {  
                        out.write(buffer, 0, readed);  
                    }  
                    else  
                    {  
                        break;  
                    }  
                }  
            }  
            finally  
            {  
                if (out != null)  
                {  
                    out.close();  
                }  
            }  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace(); 
        }  
    
	    
	}
	
	
}
